#!/bin/bash
# faceapi RPC 服务配置
# author    : guyadong
# date      : 2020/08/24
# 导入标志,确保只会被导入一次
faceapi_service_config_included=true
# 脚本初始化
[ "$common_init_included" != 'true' ] && . ./common_init.sh
faceapi_servicecfg_title='FaceApi RPC 服务配置'

service_online faceapi_service $DEFAULT_FACEAPI_PORT $faceapi_service_port

function fun_bind_faceapi_service(){    
    [ $context_facelog_service_configured -eq 0 ] && [ $context_last_code -ne 3 ] && return 4
    [ $context_facelog_service_configured -eq 0 ] && [ $context_last_code -eq 3 ] && return 3
    local defaultno=    
    [ "$faceapi_service_enable" = 1 ] && defaultno='--defaultno'

    dialog  --backtitle "$DEFAULT_BACK_TITLE$context_backtitle_suffix" $DLG_YESNO_LABEL \
            --title "$faceapi_servicecfg_title" $DLG_BACK_BTN $defaultno \
            --yesno "是否绑定 FaceApi RPC 服务?" 5 30
    local code=$?
    [ $code -lt 2 ] && faceapi_service_enable=$code
    return $code
}

# 检查连接参数是否有无效值,有则返回-1,无则返回0
# $1 $host
# $2 $port
# $3 $sdkversion
function _check_faceapi_service_params(){
    [[ ! "$1" =~ ^[A-Za-z0-9_\.]+$ ]] && error_message "主机名(host)无效" && return 255
    [[ ! "$2" =~ ^[0-9]{1,5}$      ]] && error_message "端口号(port)不可为空,必须为数字" && return 255
    [[ ! "$3" =~ ^[A-Za-z0-9]+$    ]] && error_message "算法版本号无效,必须为字母和数字 $3" && return 255
    return 0
}

# 输出 facelog RPC 服务连接参数值
function output_faceapi_service_params(){
    echo faceapi_service_host=$faceapi_service_host
    echo faceapi_service_port=$faceapi_service_port
}
# 设置 facelog RPC 服务连接参数
# 从 faceapi_service_host faceapi_service_port 等指向的变量名中读取连接参数作为默认输入
# 对话框结束时回写到对应的变量
# 如果参数无效则一直循环,如果点返回则跳出循环返回3,否则返回0
function fun_faceapi_service_connect(){
    local host="$faceapi_service_host"
    local port="$faceapi_service_port"
    local sdkversion="$faceapi_service_sdkversion"
    while true  ; 
    do 
        # 保存对话框选项的值到$values,如果对话框选项返回,则$values值为'EXIT_$?'
        local values=$(dialog \
            --backtitle "$DEFAULT_BACK_TITLE$context_backtitle_suffix" \
            --title '配置 faceapi RPC 服务连接参数' $DLG_BACK_BTN --nocancel \
            --form "faceapi RPC 服务是独立运行的人脸识别算法服务,可为facelog服务提供远程人脸识别功能,\
请输入faceapi RPC 服务连接参数,你要确保facelog服务运行时指定的RPC服务连接可用,否则facelog服务无法使用faceapi RPC 服务:" 15 66 0 \
                "主机名(host)    :"  1 1 "$host" 	    1 24 34 0 \
                "端口号(port)    :"  2 1 "$port"        2 24 34 0 \
                "算法版本号       :"  3 1 "$sdkversion"  3 24 34 0 \
                3>&1 1>&2 2>&3 || echo EXIT_$?)
        
        [[ "$values" =~ EXIT_[0-9] ]] && return ${values##*EXIT_}
        host="$(echo      "$values" | sed -n 1p)"
        port="$(echo      "$values" | sed -n 2p)"
        sdkversion="$(echo      "$values" | sed -n 3p)"
        ! _check_faceapi_service_params "$host" "$port" "$sdkversion" && continue
        # 按确认按钮时将对话框返回值分别保存到对应的变量中
        faceapi_service_host="$host"
        faceapi_service_port="$port"
        faceapi_service_sdkversion="$sdkversion"
        context_facelog_service_configured=0
        return 0
    done
}

function fun_local_faceapi_service_connect(){
    [ -z "$faceapi_service_host" ] && faceapi_service_host=$DEFAULT_FACEAPI_HOST
    [ $faceapi_service_online -eq 0 ] && faceapi_service_port=$faceapi_service_online_port
    [ $faceapi_service_online -ne 0 ] && faceapi_service_port=$DEFAULT_FACEAPI_PORT
    [ -z "$faceapi_service_sdkversion" ] && faceapi_service_sdkversion=$DEFAULT_SDK_VERSION
    fun_faceapi_service_connect
}

#fun_local_faceapi_service_connect
#output_faceapi_service_params